// pages/placing/address-from.js
var util = require('../../network_util.js');
var config = require('../../config.js');
var commonjs = require('../../common.js');
var amapFile = require('../../libs/amap-wx.js');
var base = require('../../base.js');
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    cityList: [],
    airportTerminals: {},
    // airPortInfo:[{name:"", terminals:"", position: ""}],
    fromInfo: "",
    localCityName: ""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // var fromInfo = app.globalData.fromInfo;
    console.log(app.globalData.fromInfo);
    this.setData({ fromInfo: app.globalData.fromInfo });
    if (commonjs.isEmpty(app.globalData.fromInfo)){
      wx.showToast({
        // title: '定位失败或当前城市未开通',
        title: '定位失败',
      })
      this.get1stCity();
      return;
    }else{
      this.getTerminalList(app.globalData.fromInfo.cityId);
    }
  },
  get1stCity: function(){
    var that = this;
    //2.获取城市列表
    util._get(config.fromCityList,
      function (res) {
        var fromInfo = {};
        if(res.data.data && res.data.data.length>0){
          var city = res.data.data[0];
          fromInfo.cityId = city.id;
          fromInfo.cityName = city.cityName;
          that.setData({ fromInfo: fromInfo });
          app.globalData.fromInfo = that.data.fromInfo;
          that.getTerminalList(that.data.fromInfo.cityId);
        }  
      }, function (res) {
        console.log("fromCityList fail");
        // console.log(res);
      })
  },
  getTerminalList: function (cityId) {
    console.log("address.10: " + cityId);
    if (cityId <= 0) {
      this.setData({ airportTerminals: [] });
      wx.showToast({
        title: '定位失败',
      })
      return;
    }
    console.log("address.12");
    var that = this;
    util._get(config.searchAddress + "?cityId=" + cityId,
      function (res) {
        // console.log(res);
        if (res.statusCode == 200) {
          if (res.data.code == 200) {
            var airportTerminals = [];
            for (var i in res.data.data) {
              var terminals = commonjs.trim(res.data.data[i].terminalBuilding, ',').split(',');
              for (var j in terminals) {
                var aT = {};
                // aT.idx = j;
                aT.cityId = cityId;
                aT.cityName = that.data.fromInfo.cityName;
                aT.airportId = res.data.data[i].id;
                aT.airportName = res.data.data[i].airportName;
                aT.terminal = res.data.data[i].airportName + terminals[j];
                aT.position = res.data.data[i].position;
                airportTerminals.push(aT);
              }
            }
            that.setData({ airportTerminals: airportTerminals });
          } else {
            wx.showToast({
              title: res.data.msg + '(' + res.data.code + ')',
            })
          }
        } else {
          wx.showToast({
            title: '获取机场航站楼 系统错误：' + res.statusCode,
          })
        }
      })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },
  onTerminalItemTap: function (e) {
    var pages = getCurrentPages();
    var currPage = pages[pages.length - 1]; //当前页面 
    var prevPage = pages[pages.length - 2]; //上一个页面 ,
    console.log("e.currentTarget: ");
    console.log(e.currentTarget);
    var fromInfo = {
      cityId: e.currentTarget.dataset.cityid,
      cityName: e.currentTarget.dataset.cityname,
      airportId: e.currentTarget.dataset.airportid,
      airportName: e.currentTarget.dataset.airportname,
      terminal: e.currentTarget.dataset.terminal,
      position: e.currentTarget.dataset.position
    }
    prevPage.setData({ fromInfo: fromInfo })
    app.globalData.fromInfo = fromInfo;
    prevPage.getDrivingRoute();
    wx.navigateBack({

    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },
  onCancle: function () {
    wx.navigateBack({

    })
  }
})